package com.pro01.backend.controller;

import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.Forum2CommentLikeService;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/forum2/comments")
public class Forum2CommentLikeController {

    @Autowired
    private Forum2CommentLikeService likeService;

    // 点赞评论
    @PostMapping("/{commentId}/like")
    public Result<Void> likeComment(@PathVariable Long commentId,
                                    HttpServletRequest request) {
        Long userId = TokenUtil.getUserId(request);
        try {
            int result = likeService.likeComment(userId, commentId);
            return result > 0 ? Result.success() : Result.error("点赞失败");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    // 取消点赞评论
    @DeleteMapping("/{commentId}/like")
    public Result<Void> unlikeComment(@PathVariable Long commentId,
                                      HttpServletRequest request) {
        Long userId = TokenUtil.getUserId(request);
        try {
            int result = likeService.unlikeComment(userId, commentId);
            return result > 0 ? Result.success() : Result.error("取消点赞失败");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
}

